package com.google.code.p.restrpc.core.async.impl;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.google.code.p.restrpc.MutableAsyncRpcResult;
import com.google.code.p.restrpc.RpcResponse;
import com.google.code.p.restrpc.client.RpcResponseHandler;

/**
 * @author dz
 */
public class DefaultAsyncRpcResult implements
                                  MutableAsyncRpcResult,
                                  Serializable {
    
    private String id;
    
    private boolean finished;
    
    private Class resultClazz;
    
    private RpcResponse rpcResponse;
    
    /**
     * @return the id
     */
    public String getId() {
        return id;
    }
    
    /**
     * @param id
     *            the id to set
     */
    public void setId(String id) {
        this.id = id;
    }
    
    /**
     * @return the finished
     */
    public boolean isFinished() {
        return finished;
    }
    
    /**
     * @param finished
     *            the finished to set
     */
    public void setFinished(boolean finished) {
        this.finished = finished;
    }
    
    /**
     * @return the resultClazz
     */
    public Class getResultClazz() {
        return resultClazz;
    }
    
    /**
     * @param resultClazz
     *            the resultClazz to set
     */
    public void setResultClazz(Class resultClazz) {
        this.resultClazz = resultClazz;
    }
    
    /**
     * @return the rpcResponse
     */
    public RpcResponse getRpcResponse() {
        return rpcResponse;
    }
    
    /**
     * @param rpcResponse
     *            the rpcResponse to set
     */
    public void setRpcResponse(RpcResponse rpcResponse) {
        this.rpcResponse = rpcResponse;
    }
    
    /**
     * 
     */
    public Object queryResult() {
        if (!finished) {
            return null;
        }
        
        return RpcResponseHandler.instance.handle(rpcResponse, resultClazz);
    }
    
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", id)
                                        .append("finished", finished)
                                        .append("resultClazz", resultClazz)
                                        .append("rpcResponse", rpcResponse)
                                        .toString();
    }
    
}
